package com.hyz.advice;

import org.apache.logging.log4j.message.ReusableMessage;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import pojo.ResponseResult;

import java.util.*;

/**
 * @author HGD
 * @date 2022/10/8 16:42
 */
@Component
@Aspect
public class WebPreAdvice {
    @Pointcut("execution(* com.hyz.controller.*.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public ResponseResult paramCheck(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            if (Objects.isNull(arg)) {
                return new ResponseResult<Void>(400, "参数为空");
            }
        }

        Object proceed = pjp.proceed();
        ResponseResult result = (ResponseResult) proceed;
        if (!Objects.isNull(result.getData()) && Objects.equals(result.getData().getClass(), ArrayList.class)) {
            Map<String, List> map = new HashMap<>();
            map.put("items", (List) result.getData());
            result.setData(map);
        }
        if (Objects.equals(result.getStatus(), 200)) {
            result.setStatus(0);
        }
        return result;
    }
}
